using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button1.Attributes.Add("onclick",
                   "return confirm('Are you sure you want to leave this page? Confirm');");
        this.Button2.Attributes.Add("onclick",
                   "return alert('Are you sure you want to leave this page? Alert');");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        string strScript = "<script language=JavaScript>";
        string strMessage = "Alert message";
        string key = null;

        if (btn != null && btn.Text.Equals("Alert"))
        {
            strScript += " alert(\"" + strMessage + "\"); ";
            strScript += " </script> ";

            key = "alertScript";
        }
        else if (btn != null )
        {
            strMessage = "Confirm message";
            //strScript += "<!-- ";
            //strScript += " function confirmation() { ";
            strScript += " var answer = confirm(\"" + strMessage + "\"); ";
            strScript += " if (answer){ ";
            strScript += " window.location = \"http://www.google.com/\"; ";
            strScript += " } ";
            strScript += " else{ ";
            strScript += " alert(\"Thanks for sticking around!\"); ";
            strScript += " } ";
            //strScript += " } ";
            //strScript += " //--> ";
            strScript += " </script> ";

            key = "confirmScript";
       }

       if (key != null)
       {
           Type t = this.GetType();
           if (!this.ClientScript.IsStartupScriptRegistered(t, key))
           {
               this.ClientScript.RegisterStartupScript(
                   t, key, strScript);
           }
       }
    }
}
